package com.itzzj.controller;

import com.itzzj.common.anno.OperateLog;
import com.itzzj.common.enums.ActionEnum;
import com.itzzj.common.response.Result;
import com.itzzj.common.response.ResultResponse;
import com.itzzj.common.utils.JwtUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "token测试类")
@RestController
@RequestMapping("/token")
public class TokenController {

    @Autowired
    private JwtUtil jwtUtil;

    @OperateLog(desc = "获取token",actionType = ActionEnum.ADMIN_GET)
    @GetMapping("getToken")
    public Result getToken(String username){
        String token = jwtUtil.generateToken(username);
        return ResultResponse.success(token);
    }

    @OperateLog(desc = "获取token中的用户名",actionType = ActionEnum.ADMIN_GET)
    @GetMapping("getUserNameByToken")
    public Result getUserNameByToken(String token){
        String name = jwtUtil.getUserNameFromToken(token);
        return ResultResponse.success(name);
    }

    @OperateLog(desc = "判断token是否到期",actionType = ActionEnum.ADMIN_GET)
    @GetMapping("isTokenExpiration")
    public Result isTokenExpiration(String token){
        boolean expiration = jwtUtil.isExpiration(token);
        return ResultResponse.success(expiration);
    }
}
